<template>
  <div>
    <div id="searchForm">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" ref="searchForm">
        <el-form-item label="" prop="headteacher">
          <el-input v-model="searchForm.headteacher" placeholder="班主任"></el-input>
        </el-form-item>
        <el-form-item label="" prop="class_room">
          <el-input v-model="searchForm.class_room" placeholder="教室"></el-input>
        </el-form-item>
        <el-form-item label="" prop="class_state">
          <el-select v-model="searchForm.class_state" placeholder="请选择状态" style="width: 100%">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="结课" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchClass">查询</el-button>
          <el-button type="primary" @click="resetSearchFormData('searchForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" plain size="small" @click="add">新增</el-button>
<!--    <el-button type="danger" plain size="small" @click="batchDel">批量删除</el-button>-->
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="class_name"
        label="班级名">
      </el-table-column>
      <el-table-column
        prop="class_room"
        label="教室">
      </el-table-column>
      <el-table-column
        prop="student_number"
        label="人数">
      </el-table-column>
      <el-table-column
        prop="headteacher"
        label="班主任">
      </el-table-column>
      <el-table-column
        prop="class_start"
        label="开班时间">
      </el-table-column>
      <el-table-column
        prop="class_state"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.class_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="正常"
            inactive-text="结课"
            @change="changeState(scope.row.id,scope.row.class_state)"
            :active-value="1"
            :inactive-value="2">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="班级名" :label-width="formLabelWidth" prop="class_name">
          <el-input v-model="form.class_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="教室" :label-width="formLabelWidth" prop="class_room">
          <el-input v-model="form.class_room" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="人数" :label-width="formLabelWidth" prop="student_number">
          <el-input v-model="form.student_number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="class_state">
          <el-select v-model="form.class_state" placeholder="请选择状态" style="width: 100%">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="结课" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班主任" :label-width="formLabelWidth" prop="headteacher">
          <el-input v-model="form.headteacher" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开班时间" :label-width="formLabelWidth" prop="class_start">
          <el-date-picker type="date"
                          placeholder="选择开班日期"
                          v-model="form.class_start"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          style="width: 100%;">
          </el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="班主任" :label-width="formLabelWidth" prop="role_id">-->
        <!--          <el-select v-model="form.role_id" placeholder="请选择角色" style="width: 100%">-->
        <!--            <el-option :label="role.role_name" :value="role.role_id" v-for="role in roles"></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpDateClazz">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

// import {addOrUpdate, changeUserState, resetPwd, UserBatchDelete, userOne, userPage} from "@/api/user";
// import {roleList} from "@/api/role";
// import {deptList} from "@/api/dept";

import {changeClazzState, clazzAddOrUpdate, clazzDelete, clazzOne, clazzPage} from "@/api/clazz";

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      tableData: [],
      multipleSelection: [],
      currentPage: 1, //当前页
      pageSizes: [5, 10, 15, 20],
      pageSize: 5, //每页条数
      total: 0,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'}
        ],
        active: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ],
        role_id: [
          {required: true, message: '请选择角色', trigger: 'blur'}
        ],
        dept_id: [
          {required: true, message: '请选择部门', trigger: 'blur'}
        ],

      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        class_name: '',
        class_room: '',
        class_state: '',
        student_number: '',
        headteacher: '',
        class_start: ''
      },
      searchForm: {
        headteacher: '',
        class_room: '',
        class_state: ''
      },
      roles: [],
      depts: []
    }
  },
  methods: {

    //搜索用户信息 分页
    searchClass() {
      this.currentPage = 1
      this.clazzPageData()
    },
    resetSearchFormData(formName) {
      this.$refs[formName].resetFields();
      this.currentPage = 1
      this.clazzPageData()
    },

    changeState(id, state) {
      changeClazzState({id, state}).then(response => {
        this.$notify.success(response.message)
        this.clazzPageData()
      })
    },
    // 编辑
    handleEdit(row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      clazzOne(row.id).then(response => {
        this.form = response.data.clazz
      })

    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
        clazzDelete(id).then(response=>{
          this.$notify.success(response.message)
          this.clazzPageData()
        })
      })
    },
    // 复选 参数  val  表示当前选中行的数据
    handleSelectionChange(val) {
      this.multipleSelection = val.map(row => row.id)
    },
    // 批量删除
    // batchDel() {
    //   if (this.multipleSelection.length == 0) {
    //     this.$notify.warning('请至少选择一条数据')
    //     return
    //   }
    //   this.$confirm('删除选中数据, 是否继续?', '删除', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {   // 确定
    //     UserBatchDelete({ids: this.multipleSelection}).then(response => {
    //       this.$notify.success(response.message)
    //       this.clazzPageData()
    //     })
    //   })
    // },
    // 新增
    add() {
      this.title = '新增'
      this.dialogFormVisible = true
    },
    addOrUpDateClazz() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          clazzAddOrUpdate(this.form).then(response => {
            this.$notify.success(response.message);
            this.clazzPageData()
            this.dialogFormVisible = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.clazzPageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.clazzPageData()
    },
    //关闭弹窗时 清除表单数据
    clearInfo() {
      this.form = {
        username: '',
        age: '',
        email: '',
        active: '',
        role_id: '',
        dept_id: ''
      }
      this.$refs.dialogForm.clearValidate()
    },
    clazzPageData() {
      clazzPage({
        page: this.currentPage,
        size: this.pageSize,
        headteacher: this.searchForm.headteacher,
        class_room: this.searchForm.class_room,
        class_state: this.searchForm.class_state
      }).then(response => {
        this.tableData = response.data.tableData
        this.total = response.data.total
      })
    },


  },
  created() {
    this.clazzPageData()
  }
}
</script>
<style scoped lang="scss">
#searchForm {
  border-bottom: 1px solid #ccc;
  margin-bottom: 15px;
}
</style>
